<?php

/*

Process Searching for Wild Pets (searching_for_pets.pro.php)

*/
ob_start();
include "global.inc.php";

$findSearch = fetch("SELECT * FROM search_pets2 WHERE userid = '$userid' AND game = '$game'");
$findSearchForPetRow = fetch("SELECT * FROM search_pets_arrays WHERE game = '$game'");

if (!$findSearch[id])
{
	die(header(error("search_for_pets.php?game=$game","You are not searching right now.")));
}

if ($findSearch[found])
{
	die(header(error("search_for_pets.php?game=$game","You cannot explore until you have finished with what you found (pet, items, nets, etc.)")));
}

if ($act == "move")
{
	if ($move == "up")
	{
		$movey = $findSearch[y_cord];
		$movex = $findSearch[x_cord] - 1;
	}
	if ($move == "down")
	{
		$movey = $findSearch[y_cord];
		$movex = $findSearch[x_cord] + 1;
	}
	if ($move == "left")
	{
		$movey = $findSearch[y_cord] - 1;
		$movex = $findSearch[x_cord];
	}
	if ($move == "right")
	{
		$movey = $findSearch[y_cord] + 1;
		$movex = $findSearch[x_cord];
	}
	if (($movey < 1) OR ($movey > 8) OR ($movex < 1) OR ($movex > 8))
	{
		die(header(error("searching_for_pets.php?game=$game","You cannot move past the edge of the map.")));
	}
	$cord = "$movex$movey";
	if (
	($cord == "16") OR
	($cord == "17") OR
	($cord == "18") OR
	($cord == "28") OR
	($cord == "41") OR
	($cord == "51") OR
	($cord == "52") OR
	($cord == "53") OR
	($cord == "54") OR
	($cord == "63") OR
	($cord == "64") OR
	($cord == "65")
	)
	{
		die(header(error("searching_for_pets.php?game=$game","You cannot move into water.")));
	}
	mysql_query("UPDATE search_pets2 SET x_cord=$movex WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET y_cord=$movey WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET direction = '$move' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET moves_left=$findSearch[moves_left]-1 WHERE userid = '$userid' AND game = '$game'");
}

$findPet = rand(1,20);
if ($findPet == 7)
{
	$pets = explode(", ", $findSearchForPetRow[pets]);

	$countThePets = count($pets) - 1;
	$randPets = rand(0,$countThePets);
	$randLevel = rand(1,6);

	mysql_query("UPDATE search_pets2 SET found = 'pet' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET found_what = '$pets[$randPets]' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET found_what2=$randLevel WHERE userid = '$userid' AND game = '$game'");
	die(header(error("search_for_pets.php?game=$game","You have found a wild pet!!!")));
}

$findItem = rand(1,45);
if ($findItem == 20)
{
	$items = explode(", ", $findSearchForPetRow[items]);

	$countitems = count($items) - 1;
	$randItem = rand(0,$countitems);

	mysql_query("UPDATE search_pets2 SET found = 'item' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET found_what = '$items[$randItem]' WHERE userid = '$userid' AND game = '$game'");
	die(header(error("search_for_pets.php?game=$game","You have found a $items[$randItem]!")));
}

$findNet = rand(1,35);
if ($findNet == 30)
{
	$nets = array(
	"Basic Net",
	"Basic Net",
	"Basic Net",
	"Basic Net",
	"Basic Net",
	"Basic Net",
	"Basic Net",
	"Basic Net",
	"Basic Net",
	"Electric Net",
	"Electric Net",
	"Electric Net",
	"Electric Net",
	"Magic Net",
	"Magic Net",
	"Magic Net",
	"Magic Net"
	);

	$countnets = count($nets) - 1;
	$randNets = rand(0,$countnets);

	mysql_query("UPDATE search_pets2 SET found = 'net' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET found_what = '$nets[$randNets]' WHERE userid = '$userid' AND game = '$game'");
	die(header(error("search_for_pets.php?game=$game","You have found a $items[$randItem]!")));

}

header("Location: searching_for_pets.php?game=$game");
?>